﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using JsonFx.Json;

public class TestAniEffect : MonoBehaviour {
	
	public float speed = 1;

	public GameObject[] effects;

	public int effectIndex;

	public GameObject[] selfs;
	public GameObject[] targets;

	protected bool selected;
	protected GameObject[] selecteds;

	// OReportEffect
	// FightReportEffect
	public class OReportEffectTest : OReportEffect {
		
		public TestAniEffect testAniEffect;

//		public override CardHeroAdapter getCardHeroAdapter(int gId) {
//			return getGameObject(gId).GetComponent<CardHeroAdapter>();
//		}
		
		public override GameObject getGameObject(int gId) {
			GameObject[] selfs = testAniEffect.selfs;
			int length = selfs == null ? 0 : selfs.Length;
			if(gId < length) {
				return testAniEffect.selfs[gId];
			}
			
			return testAniEffect.targets[gId - length];
		}
	}

	void Start () {
		Application.targetFrameRate=60;
		AniBase.Speed = speed;
		// OReportEffectTest
		OReportEffectTest reportEffectTest = new OReportEffectTest();
		reportEffectTest.initEffects();
		reportEffectTest.testAniEffect = this;
		OReportEffect.Instance = reportEffectTest;
	}

	public int getGameObjectId(GameObject go) {
		int gId = 0;
		int length = selfs.Length;
		for(; gId < length; gId++) {
			if(go == selfs[gId]) {
				return gId;
			}
		}

		length = targets.Length;
		for(int i = 0; i < length; i++) {
			if(go == targets[i]) {
				return gId;
			}

			gId++;
		}

		return 0;
	}

	static private int buffIndex;

	void playEffect(GameObject self){
		int length = effects == null ? 0 : effects.Length;
		if(effectIndex > length) {
			effectIndex -= length;
		}

		if(effectIndex < 0) {
			return;
		}


		GameObject effect = Instantiate(effects[effectIndex]) as GameObject;
		//Debug.Log(" effect:"+effect);
		effect.transform.localScale = Vector3.one;
		GameObject[] targets = selected && selecteds != null ? selecteds : this.targets;
		length = targets.Length;
		OReportDetail reportDetail = new OReportDetail();
		reportDetail.self = getGameObjectId(self);
		reportDetail.targets = new int[length];
		for(int  i = 0; i < length; i++) {
			reportDetail.targets[i] = getGameObjectId(targets[i]);
		}

		List<OReportDetail>[] details = new List<OReportDetail>[length];
		for(int  i = 0; i < length; i++) {
			details[i] = new List<OReportDetail>();
		}

		try {
			string effectName = effect.name.Replace("Effect_", "");
			effectName = effectName.Replace("(Clone)", "");
			Debug.Log(" effectName:" + effectName);
			GameObject effectBuff = Instantiate(Resources.Load("Effect_" + effectName + "_AB")) as GameObject;
			for(int  i = 0; i < length; i++) {
				OReportDetail detail = new OReportDetail();
				detail.self = reportDetail.targets[i];
				detail.effect = "AB";
				Dictionary<string, object> buff = new Dictionary<string, object>();
				buff.Add("id", buffIndex++);
				buff.Add("name", effectName);
				detail.effectData = buff;
				details[i].Add(detail);
			}

			Destroy(effectBuff);

		} catch(System.Exception e) {
		}

		for(int  i = 0; i < length; i++) {
			for(int j = Random.Range(0, 5); j > 0; j--) {
				OReportDetail detail = new OReportDetail();
				detail.self = reportDetail.targets[i];
				detail.effectData = Random.Range(1, 99);
				switch(Random.Range(0, 3)) {
				case 0:
					detail.effect = "R";
					break;
				default:
					break;
				}
				
				details[i].Add(detail);
			}
		}

		object[] effectDatas = new object[]{details};
		effectDatas = JsonReader.Deserialize(JsonWriter.Serialize(effectDatas), typeof(object[])) as object[];
		reportDetail.effectData = effectDatas[0];

		Debug.Log("reportDetail.effectData:" + UnityORM.Json.Serialize(reportDetail.effectData));
		effect.GetComponent<AniEffectC>().play(reportDetail, ()=>{Debug.Log("animation play complete!");});
	}

	public void playPreEffect(){
		if(effects != null) {
			if(--effectIndex < 0){
				effectIndex += effects.Length;
				
			}
		}
	}
	
	public void playNextEffect(){
		if(effects != null) {
			int length = effects.Length;
			if(++effectIndex >= length){
				effectIndex -= length;
			}
		}
	}

	public void clearAllBuff(GameObject self) {
		OReportEffect.Instance.clearAllBuff(getGameObjectId(self));
	}

	public void clearAllBuffs() {
		OReportEffect.Instance.clearAllBuffs();
	}
}
